/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.cell;

import com.daikit.daikit4gxt.client.ui.fields.MyReadOnlyField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.sencha.gxt.cell.core.client.form.TextInputCell.TextFieldAppearance;
import com.sencha.gxt.cell.core.client.form.ValueBaseInputCell;
import com.sencha.gxt.cell.core.client.form.ViewData;
import com.sencha.gxt.core.client.dom.XElement;
import com.sencha.gxt.data.shared.LabelProvider;


/**
 * A cell for {@link MyReadOnlyField}
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <T>
 */
public class MyReadOnlyFieldCell<T> extends ValueBaseInputCell<T>
{

	private final LabelProvider<T> labelProvider;

	/**
	 * Constructor
	 * 
	 * @param labelProvider
	 *           the labelProvider
	 */
	public MyReadOnlyFieldCell(final LabelProvider<T> labelProvider)
	{
		super(GWT.<TextFieldAppearance> create(TextFieldAppearance.class));
		setReadOnly(true);
		this.labelProvider = labelProvider;
	}

	@Override
	public void render(final com.google.gwt.cell.client.Cell.Context context, final T value, final SafeHtmlBuilder sb)
	{
		final String toString = labelProvider.getLabel(value);
		final ViewData viewData = checkViewData(context, toString);
		final String s = (viewData != null) ? viewData.getCurrentValue() : toString;

		final FieldAppearanceOptions options = new FieldAppearanceOptions(getWidth(), getHeight(), isReadOnly(), getEmptyText());
		options.setName(name);
		options.setDisabled(isDisabled());
		((TextFieldAppearance) getAppearance()).render(sb, "text", s == null ? "" : s, options);
	}

	@Override
	public void setSize(final XElement parent, final int width, final int height)
	{
		super.setSize(parent, width, height);
		((TextFieldAppearance) getAppearance()).onResize(parent, width, height);
	}

}
